package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.ArithUtils;
import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.OutStorageDetailBatchListener;
import com.inossem.warehousestorage_inossem.utils.CYChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.EditTextWatcher;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class OutStorageDetailBatchListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private OutStorageDetailBatchListener mOutStorageDetailBatchListener;
    private List<BatchBean.BatchInfoBean> mBatchList;
    private String mType, mBatchNumber, mActualSendNumber;
    private EditText batchNumberEditText;

    private ItemListBean mMaterielDetail;

    private TextView outStorageNumberHead;

    public List<BatchBean.BatchInfoBean> getmBatchList() {
        return mBatchList;
    }

    public String getmActualSendNumber() {
        mActualSendNumber = calculatedTotal();
        return mActualSendNumber;
    }

    public OutStorageDetailBatchListAdapter(Context context, ItemListBean materielDetail, List<BatchBean.BatchInfoBean> batchList, String batchNumber, String type, OutStorageDetailBatchListener outStorageDetailBatchListener) {
        mContext = context;
        mMaterielDetail = materielDetail;
        mBatchList = batchList == null ? new ArrayList<>() : batchList;
        mBatchNumber = batchNumber;
        mType = type;
        mOutStorageDetailBatchListener = outStorageDetailBatchListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mBatchList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mBatchList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            TextView materielCode, materielDescription, demandNumber, inventoryAddress, sendedNumber;

            ImageView search;

            switch (mType) {
                case OutStorageUtils.OUT_STORAGE_TYPE_PICKING:
                    //领料出库
                    convertView = inflater.inflate(R.layout.view_out_storage_detail_first_item, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    demandNumber = (TextView) convertView.findViewById(R.id.demandNumber);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    sendedNumber = (TextView) convertView.findViewById(R.id.sendedNumber);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);
                    batchNumberEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    search = (ImageView) convertView.findViewById(R.id.search);
                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    demandNumber.setText(mMaterielDetail.getOrder_qty() + mMaterielDetail.getUnit_name());
                    sendedNumber.setText(mMaterielDetail.getSend_qty() + mMaterielDetail.getUnit_name());
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());
                    inventoryAddress.setText(mMaterielDetail.getLocation_code() + " - " + mMaterielDetail.getLocation_name());
                    batchNumberEditText.setText(mBatchNumber);
                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(batchNumberEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入批次号");
                            } else {
                                mOutStorageDetailBatchListener.onGetBatch(batchNumberEditText.getText().toString());
                            }
                        }
                    });
                    break;
                case OutStorageUtils.OUT_STORAGE_TYPE_SALE:
                    //销售出库
                    break;
                case OutStorageUtils.OUT_STORAGE_TYPE_PRODUCTION:
                    //生产领用
                    convertView = inflater.inflate(R.layout.view_out_storage_detail_first_item, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    demandNumber = (TextView) convertView.findViewById(R.id.demandNumber);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    sendedNumber = (TextView) convertView.findViewById(R.id.sendedNumber);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);
                    batchNumberEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    search = (ImageView) convertView.findViewById(R.id.search);
                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    demandNumber.setText(mMaterielDetail.getOrder_qty() + mMaterielDetail.getUnit_name());
                    sendedNumber.setText(mMaterielDetail.getSend_qty() + mMaterielDetail.getUnit_name());
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());
                    inventoryAddress.setText(mMaterielDetail.getLocation_code() + " - " + mMaterielDetail.getLocation_name());
                    batchNumberEditText.setText(mBatchNumber);
                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(batchNumberEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入批次号");
                            } else {
                                mOutStorageDetailBatchListener.onGetBatch(batchNumberEditText.getText().toString());
                            }
                        }
                    });
                    break;
                case OutStorageUtils.OUT_STORAGE_TYPE_ALLOCATION:
                    convertView = inflater.inflate(R.layout.cy_view_out_storage_allocation_listview_first, null);
                    materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                    materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                    inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.outStorageNumber);

                    materielCode.setText(mMaterielDetail.getMat_code());
                    materielDescription.setText(mMaterielDetail.getMat_name());
                    inventoryAddress.setText(String.valueOf(mMaterielDetail.getLocation_name()));
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());

                    batchNumberEditText = (EditText) convertView.findViewById(R.id.batchNumber);
                    search = (ImageView) convertView.findViewById(R.id.search);

                    search.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search);
                            if (TextUtils.isEmpty(batchNumberEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入批次号");
                            } else {
                                mOutStorageDetailBatchListener.onGetBatch(batchNumberEditText.getText().toString());
                            }
                        }
                    });

                    break;
                case CYChangeStorageUtils.CHANGE_STORAGE_TYPE:
                    convertView = inflater.inflate(R.layout.cy_view_change_storage_material_detail_first_item, null);
                    batchNumberEditText = (EditText) convertView.findViewById(R.id.code);
                    ImageView search1 = (ImageView) convertView.findViewById(R.id.search);
                    batchNumberEditText.setHint("请输入批次");
                    TextView materialCode = (TextView) convertView.findViewById(R.id.materialCode);
                    TextView materialDescribe = (TextView) convertView.findViewById(R.id.materialDescribe);
                    TextView location = (TextView) convertView.findViewById(R.id.location);
                    TextView orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
                    outStorageNumberHead = (TextView) convertView.findViewById(R.id.changeStorageNumber);
                    TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
                    TextView orderNumberUnit = (TextView) convertView.findViewById(R.id.orderNumberUnit);
                    orderNumberUnit.setText(mMaterielDetail.getUnit_name());
//                        TextView changeStorageNumberUnit = (TextView) convertView.findViewById(R.id.changeStorageNumberUnit);
//                        changeStorageNumberUnit.setText(mMaterielDetail.getUnit_name());
                    materialCode.setText(mMaterielDetail.getMat_code());
                    materialDescribe.setText(mMaterielDetail.getMat_name());
                    location.setText(CYChangeStorageUtils.getLocation(mContext));
                    orderNumber.setText(mMaterielDetail.getOrder_qty());
                    mActualSendNumber = calculatedTotal();
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + mMaterielDetail.getUnit_name());
                    supplier.setText(mMaterielDetail.getSupplier_name());
                    if (!TextUtils.isEmpty(mBatchNumber)) {
                        batchNumberEditText.setText(mBatchNumber);
                    }
                    search1.setOnClickListener(new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Utils.closeSoftKeyboard(mContext, search1);
                            if (TextUtils.isEmpty(batchNumberEditText.getText().toString())) {
                                Utils.middleToast(mContext, "请输入批次号");
                            } else {
                                mOutStorageDetailBatchListener.onGetBatch(batchNumberEditText.getText().toString());
                            }

                        }
                    });
                    break;
            }

        } else {
            BatchBean.BatchInfoBean outStorageBatchItem = mBatchList.get(position - 1);
            convertView = inflater.inflate(R.layout.view_out_storage_detail_batch, null);
            TextView stockId = (TextView) convertView.findViewById(R.id.stockId);
            TextView positionTextView = (TextView) convertView.findViewById(R.id.position);
            TextView batch = (TextView) convertView.findViewById(R.id.batch);
            TextView supplierBatch = (TextView) convertView.findViewById(R.id.supplierBatch);
            TextView stockNumber = (TextView) convertView.findViewById(R.id.stockNumber);
            EditText outStorageNumber = (EditText) convertView.findViewById(R.id.outStorageNumber);
            TextView unit = (TextView) convertView.findViewById(R.id.unit);
            String unitStr = outStorageBatchItem.getUnit_name();
            stockId.setText(outStorageBatchItem.getStock_id());
            positionTextView.setText(outStorageBatchItem.getPosition_code());
            stockNumber.setText("批次数量 " + outStorageBatchItem.getStock_qty() + unitStr);
            batch.setText(outStorageBatchItem.getBatch());
            supplierBatch.setText(outStorageBatchItem.getSupplier_batch());
            outStorageNumber.setText(outStorageBatchItem.getQty());
            unit.setText(unitStr);
            outStorageNumber.setOnFocusChangeListener((View v, boolean hasFocus) -> {
                if (!hasFocus) {
                    String outStorageNumberStr = outStorageNumber.getText().toString();
//                    检测当前仓位出库数量是否为空
                    if (!TextUtils.isEmpty(outStorageNumberStr)) {
                        //正则表达式校验输入框内容
                        if (RegexUtils.checkDecimal(outStorageNumberStr)) {
                            //检测输入数量是否超过当前仓位库存
                            if (OutStorageUtils.checkInventory(outStorageBatchItem.getStock_qty(), outStorageNumberStr)) {
                                outStorageBatchItem.setQty(outStorageNumberStr);
                                mActualSendNumber = calculatedTotal();
                            } else {
                                Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_out_storage_allocating_detail_exceed_current_position_inventory));
                                outStorageNumber.setText("");
                                outStorageBatchItem.setQty("");
                                mActualSendNumber = calculatedTotal();
                            }
                        } else {
                            outStorageBatchItem.setQty("");
                            mActualSendNumber = calculatedTotal();
                        }
                    } else {
                        outStorageBatchItem.setQty("");
                        mActualSendNumber = calculatedTotal();
                    }
                    outStorageNumberHead.setText(TextUtils.isEmpty(mActualSendNumber) ? "" : mActualSendNumber + unitStr);
                }
            });
            outStorageNumber.addTextChangedListener(new EditTextWatcher() {
                @Override
                public void onAfterTextChanged(String text) {
                    outStorageBatchItem.setQty(text);
                }
            });

            convertView.setOnLongClickListener((View v) -> {
                DialogUtils.getConfirmDialog(mContext, mContext.getResources().getString(R.string.activity_out_storage_allocating_detail_determine_to_delete_the_position) +
                        outStorageBatchItem.getPosition_code() + mContext.getResources().getString(R.string.activity_out_storage_allocating_detail_batch) + outStorageBatchItem.getBatch() + mContext.getResources().getString(R.string.activity_out_storage_allocating_detail_the_row_project), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        dialog.cancel();
                        mOutStorageDetailBatchListener.onDeleteBatchItem(outStorageBatchItem);
                    }
                }).show();
                return true;
            });
        }
        return convertView;
    }

    //计算实发数量
    private String calculatedTotal() {
        double result = 0D;
        for (int i = 0; i < mBatchList.size(); i++) {
            BatchBean.BatchInfoBean batchItem = mBatchList.get(i);
            double actualSendNumberDou;
            if (TextUtils.isEmpty(batchItem.getQty())) {
                actualSendNumberDou = 0D;
            } else {
                actualSendNumberDou = Double.parseDouble(batchItem.getQty());
            }
            result = ArithUtils.add(result, actualSendNumberDou);
        }
        return result == 0D ? "" : String.valueOf(result);
    }

}
